﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzDesk.Clients.Utility;
using EzDesk.Core.Model;
using EzDesk.Core.ServiceImpl.Services;
using EzDesk.Utility.Web.Controllers;
using EzDesk.Utility.ServiceStatusInfo;
using EzDesk.Utility.Extensions;

namespace EzDesk.ServiceHost.Controllers
{
    public class CoreController : EzDeskController
    {
        public CoreClientService _coreClientService = new CoreClientService();

        [HttpGet]
        public JsonResult Error()
        {
            var statusInfo = new StatusInfo(ServiceStatus.Error, "没有登录");
            return Json(statusInfo, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Core/TestLogin
        // RETURN_TYPE: StatusInfo

        [HttpGet]
        [Authorize]
        public JsonResult TestLogin()
        {
            var statusInfo = new StatusInfo(ServiceStatus.Success, "已登录");
            return Json(statusInfo, JsonRequestBehavior.AllowGet);
        }

        #region [ Globle Info ]

        //
        // GET: /Core/OrganizationInfo
        // RETURN_TYPE: Organization

        [HttpGet]
        [Authorize]
        public JsonResult OrganizationInfo()
        {
            return Json(
                _coreClientService.GetOrganizationInfo(),
                JsonRequestBehavior.AllowGet
                );
        }

        #endregion

        #region [ App Functions ]
        //
        // GET: /Core/AppFunctions
        // RETURN_TYPE: IList<AppFunction>

        [HttpGet]
        [Authorize]
        public JsonResult AllAppFunctions()
        {
            return Json(
                _coreClientService.GetAllAppFunctions(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }

        
        //
        // GET: /Core/AllAppFunctionTypes
        // RETURN_TYPE: IList<AppFunctionType>

        [HttpGet]
        [Authorize]
        public JsonResult AllAppFunctionTypes()
        {
            return Json(
                _coreClientService.GetAllAppFunctionTypes(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/AppFunctionsByTypeName
        // RETURN_TYPE: IList<AppFunction>

        [HttpGet]
        [Authorize]
        public JsonResult AppFunctionsByTypeName(string appFunctionTypeName)
        {
            return Json(
                _coreClientService.GetAppFunctionsByTypeName(CurrentUserId, appFunctionTypeName),
                JsonRequestBehavior.AllowGet
                );
        }



        //
        // GET: /Core/AllAppFunctionCategories
        // RETURN_TYPE: IList<AppFunctionType>

        [HttpGet]
        [Authorize]
        public JsonResult AllAppFunctionCategories()
        {
            return Json(
                _coreClientService.GetAllAppFunctionCategories(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/AppFunctionsByCategoryName
        // RETURN_TYPE: IList<AppFunction>

        [HttpGet]
        [Authorize]
        public JsonResult AppFunctionsByCategoryName(string name)
        {
            return Json(
                _coreClientService.GetAppFunctionsByCategoryName(CurrentUserId, name),
                JsonRequestBehavior.AllowGet
                );
        }


        //
        // GET: /Core/AppFunctionShortcutIds
        // RETURN_TYPE string

        [HttpGet]
        [Authorize]
        public JsonResult AppFunctionShortcutIds()
        {
            return Json(
                _coreClientService.GetAppFunctionShortcutIds(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }
        
        //
        // POST: /Core/
        // RETURN_TYPE: StatusInfo

        [HttpPost]
        [Authorize]
        public JsonResult UpdateAppFunctionShortcutSequnce(string shortCutIdSequnces)
        {
            _coreClientService.UpdateAppFunctionShortcutSequnce(CurrentUserId, shortCutIdSequnces);
            var statusInfo = new StatusInfo();
            return Json(statusInfo);
        }


        
        #endregion

        #region [ Feed Methods]

        //
        // GET: /Core/XmlFeedsBetweenDate
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult XmlFeedsBetweenDate(string feedTypeStr, DateTime from, DateTime to, bool? isRead, bool? isAvaiable, bool? isMark)
        {
            return Json(
                _coreClientService.GetXmlFeedsBetweenDate(feedTypeStr, CurrentUserId, from, to, isRead, isAvaiable, isMark),
                JsonRequestBehavior.AllowGet
                );
        }


        //
        // GET: /Core/XmlFeedsByCount
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult XmlFeedsByCount(string feedTypeStr, int startIndex, int count, bool? isRead, bool? isAvaiable, bool? isMark)
        {
            return Json(
                _coreClientService.GetXmlFeedsByCount(feedTypeStr, CurrentUserId, startIndex, count, isRead, isAvaiable, isMark),
                JsonRequestBehavior.AllowGet
                );
        }


        //
        // GET: /Core/AllXmlFeedsBetweenDate
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult AllXmlFeedsBetweenDate(DateTime from, DateTime to, bool? isRead = null, bool? isAvaiable = true, bool? isMark = null)
        {
            return Json(
                _coreClientService.GetAllXmlFeedsBetweenDate(CurrentUserId, from, to, isRead, isAvaiable, isMark),
                JsonRequestBehavior.AllowGet
                );
        }


        //
        // GET: /Core/AllXmlFeedsByCount
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult AllXmlFeedsByCount(int startIndex, int count, bool? isRead = null, bool? isAvaiable = true, bool? isMark = null)
        {
            return Json(
                _coreClientService.GetAllXmlFeedsByCount(CurrentUserId, startIndex, count, isRead, isAvaiable, isMark),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/AllJsonFeedsBetweenDate
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult AllJsonFeedsBetweenDate(long fromEpoch, long toEpoch, bool? isRead = null, bool? isAvaiable = true, bool? isMark = null)
        {
            var from = EpochDateTimeExtension.FromEpoch(fromEpoch);
            var to = EpochDateTimeExtension.FromEpoch(toEpoch);

            var feeds = _coreClientService.GetAllXmlFeedsBetweenDate(CurrentUserId, from, to, isRead, isAvaiable, isMark);

            var feedList = FeedCollection.Create(feeds, "desktop-html");
            return Json(feedList, JsonRequestBehavior.AllowGet);

        }


        //
        // GET: /Core/AllJsonFeedsByCount
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult AllJsonFeedsByCount(int startIndex, int count, bool? isRead = null, bool? isAvaiable = true, bool? isMark = null)
        {
            // 获得最新的几条Notifications
            var feeds = _coreClientService.GetAllXmlFeedsByCount(CurrentUserId, startIndex, count, isRead, isAvaiable, isMark);

            var feedList = FeedCollection.Create(feeds, "desktop-html");
            return Json(feedList, JsonRequestBehavior.AllowGet);
        }


        //
        // POST: /Core/SetUserStatus
        // RETURN_TYPE: StatusInfo

        [HttpPost]
        [Authorize]
        public JsonResult SetUserStatus(Guid feedId)
        {
            _coreClientService.SetUserStatus(CurrentUserId, feedId);
            var statusInfo = new StatusInfo();
            return Json(statusInfo);
        }

        //
        // GET: /Core/UserStatus
        // RETURN_TYPE: Guid

        [HttpGet]
        [Authorize]
        public JsonResult UserStatus()
        {
            return Json(
                _coreClientService.GetUserStatus(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // POST: /Core/UpdateFeedStatus
        // RETURN_TYPE: StatusInfo

        [HttpPost]
        [Authorize]
        public JsonResult UpdateFeedStatus(Guid feedId, bool? isRead, bool? isMark, bool? isAvailable, string memberstring)
        {
            var feeds = new List<FeedStatus>()
                            {
                                new FeedStatus()
                                    {
                                        FeedId = feedId,
                                        IsAvailable = isAvailable,
                                        IsMark = isMark,
                                        IsRead = isRead,
                                    }
                            };
            _coreClientService.UpdateFeedStatus(feeds, memberstring);
            var statusInfo = new StatusInfo();
            return Json(statusInfo);
        }

        #endregion

        #region [ UserProfile Method ]


        //
        // GET: /Core/CurrentUser
        // RETURN_TYPE: SimpleUser

        [HttpGet]
        [Authorize]
        public JsonResult CurrentUser()
        {
            return Json(
                _coreClientService.GetSimpleUserById(CurrentUserId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/UserProfileById
        // RETURN_TYPE: UserProfile

        [HttpGet]
        [Authorize]
        public JsonResult UserProfileById(Guid userId)
        {
            return Json(
                _coreClientService.GetUserProfileById(userId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/SimpleUserById
        // RETURN_TYPE: SimpleUser

        [HttpGet]
        [Authorize]
        public JsonResult SimpleUserById(Guid userId)
        {
            return Json(
                _coreClientService.GetSimpleUserById(userId),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/SimpleUserInfos
        // RETURN_TYPE: IList<SimpleUser>

        [HttpGet]
        [Authorize]
        public JsonResult SimpleUserInfos(string memberstring)
        {
            return Json(
                _coreClientService.GetSimpleUserInfos(memberstring),
                JsonRequestBehavior.AllowGet
                );
        }

        //
        // GET: /Core/DeptById
        // RETURN_TYPE: Dept

        [HttpGet]
        [Authorize]
        public JsonResult DeptById(Guid deptId)
        {
            return Json(
                _coreClientService.GetDeptById(deptId),
                JsonRequestBehavior.AllowGet
                );
        }

        #endregion

        #region [ Clients Management ]
        //
        // GET: /Core/ClientBaseUrl
        // RETURN_TYPE: string

        [HttpGet]
        [Authorize]
        public JsonResult ClientBaseUrl()
        {
            return Json(
                _coreClientService.GetClientBaseUrl(), 
                JsonRequestBehavior.AllowGet
                );
        }
        #endregion
    }
}
